﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Workflows.Components.Rules.Properties;

namespace Workflows.Components.Rules
{
    /// <summary>
    /// evaluate condition by calling a method from a assembly
    /// </summary>
    [Serializable]
    public class ClassRuleCondition:RuleNodeDataBase
    {
        #region members
        private string _EvaluableAssemblyFilePath;
        /// <summary></summary>
        public string EvaluableAssemblyFilePath
        {
            get
            {
                return this._EvaluableAssemblyFilePath;
            }
            set
            {
                this._EvaluableAssemblyFilePath = value;
            }
        }

        private string _EvaluableTypeName;
        /// <summary></summary>
        public string EvaluableTypeName
        {
            get
            {
                return this._EvaluableTypeName;
            }
            set
            {
                this._EvaluableTypeName = value;
            }
        }

        private string _AssemblyFilePath;
        /// <summary></summary>
        public string AssemblyFilePath
        {
            get
            {
                return this._AssemblyFilePath;
            }
            set
            {
                this._AssemblyFilePath = value;
            }
        }

        private string _ClassName;
        /// <summary></summary>
        public string ClassName
        {
            get
            {
                return this._ClassName;
            }
            set
            {
                this._ClassName = value;
            }
        }

        private string _MethodName;
        /// <summary></summary>
        public string MethodName
        {
            get
            {
                return this._MethodName;
            }
            set
            {
                this._MethodName = value;
            }
        }
        #endregion

        /// <summary>
        /// 
        /// </summary>
        public ClassRuleCondition():base()
        {
            this.Name = "Class condition";
            this.Description = "Evaluate object by calling method from an assembly";
            this.Img = Resources.Search;
            this.RuleNodeType = Rules.RuleNodeType.Conditional;

            this._EvaluableAssemblyFilePath = string.Empty;
            this._EvaluableTypeName = string.Empty;
            this._MethodName = string.Empty;
            this._AssemblyFilePath = string.Empty;
            this._ClassName = string.Empty;
        }

        public override System.Xml.XmlNode Serialize(ref System.Xml.XmlDocument xDoc, ref System.Xml.XmlNode parentNode)
        {
            XmlNode xNode = base.Serialize(ref xDoc, ref parentNode);

            XmlDataUtil.UpdateAttribute(ref xDoc,xNode,"EvaluableAssemblyFilePath",this.EvaluableAssemblyFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc,xNode,"EvaluableTypeName",this.EvaluableTypeName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "AssemblyFilePath", this.AssemblyFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ClassName", this._ClassName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "MethodName", this.MethodName);

            return xNode;
        }

        public override RuleNodeDataBase Instantiate(XmlNode xNode)
        {
            ClassRuleCondition classRuleCondition = new ClassRuleCondition();
            classRuleCondition.RuleNodeID = new Guid(xNode.Attributes.GetNamedItem("RuleNodeID").Value);
            classRuleCondition.Name = xNode.Attributes.GetNamedItem("Name").Value;
            classRuleCondition.Description = xNode.Attributes.GetNamedItem("Description").Value;
            classRuleCondition.RuleNodeType = (RuleNodeType) int.Parse(xNode.Attributes.GetNamedItem("RuleNodeType").Value);

            classRuleCondition.EvaluableAssemblyFilePath =
                xNode.Attributes.GetNamedItem("EvaluableAssemblyFilePath").Value;
            classRuleCondition.EvaluableTypeName = xNode.Attributes.GetNamedItem("EvaluableTypeName").Value;
            classRuleCondition.AssemblyFilePath = xNode.Attributes.GetNamedItem("AssemblyFilePath").Value;
            classRuleCondition.ClassName = xNode.Attributes.GetNamedItem("ClassName").Value;
            classRuleCondition.MethodName = xNode.Attributes.GetNamedItem("MethodName").Value;

            return classRuleCondition;
        }

        public override string DescribeInHtml()
        {
            return string.Empty;
        }

        public override string Describe()
        {
            return string.Empty;
        }

        
    }
}
